package color;
import java.awt.image.BufferedImage;

/**
 * 图形类
 */
public class AdjustFilter extends ReadAndTrans {
    //亮度
    private double brightness;
    //对比度
    private double contrast;
    //饱和度
    private double saturation;

    /**
     * 获取亮度
     * @return 亮度
     */
    public double getBrightness() {
        return brightness;

    }

    /**
     * 设置亮度
     * @param brightness 亮度
     */
    public void setBrightness(double brightness) {
        this.brightness = brightness;

    }
    /**
     * 获取饱和度
     * @return 饱和度
     */
    public double getSaturation() {
        return saturation;

    }
    /**
     * 设置饱和度
     * @param saturation 饱和度
     */
    public void setSaturation(double saturation) {
        this.saturation = saturation;

    }
    /**
     * 获取对比度
     * @return 对比度
     */
    public double getContrast() {
        return contrast;

    }
    /**
     * 设置对比度
     * @param contrast 对比度
     */
    public void setContrast(double contrast) {
        this.contrast = contrast;

    }

    /**
     * 图像容器
     * @param src 原图
     * @param dest 调整后图片
     * @return 调整后图片
     */
    public BufferedImage filter(BufferedImage src,BufferedImage dest){
        //调整各系数取值范围
        handleParameters();
        //设置宽度
        int width = src.getWidth();
        //设置高度
        int height = src.getHeight();
        //调整后图片如果为空
        if(dest == null){
            //创建调整后图片
            dest = creatCompatibleDestImage(src,null);

        }
        //设置参数
        int[] inpixels = new int[width*height];
        int[] outpixels = new int[width*height];

        //获取rgb
        getRGB(src,0,0,width,height,inpixels);

        int index = 0;
        //设置
        for(int row=0;row<height;row++){
            int ta = 0,tr = 0,tg = 0,tb = 0;
            for(int col=0;col<width;col++){
                index = row*width+col;
                ta = (inpixels[index] >> 24) & 0xff;
                tr = (inpixels[index] >> 16) & 0xff;
                tg = (inpixels[index] >> 8) & 0xff;
                tb = inpixels[index] & 0xff;

                //RGB转换为HSL色彩空间
                double[] hsl = rgb2Hsl(new int[]{tr,tg,tb});

                //调整饱和度
                hsl[1] = hsl[1]*saturation;
                if(hsl[1]<0.0){
                    hsl[1] = 0.0;
                }
                if(hsl[1]>255.0){
                    hsl[1] = 255.0;
                }

                //调整亮度
                hsl[2] = hsl[2]*brightness;
                if(hsl[2]<0.0){
                    hsl[2] = 0.0;
                }
                if(hsl[2]>255.0){
                    hsl[2] = 255.0;
                }

                //HSL转换为rgb空间
                int[] rgb = hsl2RGB(hsl);
                tr = clamp(rgb[0]);
                tg = clamp(rgb[1]);
                tb = clamp(rgb[2]);

                //调整对比度
                double cr = ((tr/255.0d)-0.5d)*contrast;
                double cg = ((tg/255.0d)-0.5d)*contrast;
                double cb = ((tb/255.0d)-0.5d)*contrast;

                //输出RGB值
                tr = (int)((cr+0.5f)*255.0f);
                tg = (int)((cg+0.5f)*255.0f);
                tb = (int)((cb+0.5f)*255.0f);

                outpixels[index] = (ta << 24) | (clamp(tr) << 16 ) | (clamp(tg) << 8) | clamp(tb);
            }
        }
        //设置rgb
        setRGB(dest,0,0,width,height,outpixels);

        return dest;
    }

    /**
     * 调整各系数取值范围
     */
    public void handleParameters(){
        //对比度
        contrast = (1.0+contrast/100.0);
        //亮度
        brightness = (1.0 +brightness/100.0);
        //饱和度
        saturation = (1.0+saturation/100.0);
    }

    public int clamp(int value){
        return value>255 ? 255:((value<0) ? 0:value);

    }

}